//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

namespace EntLibContrib.Query.Configuration.Design
{
	/// <summary>
	/// Represents the design manager that will register the design time information for the Query Application Block.
	/// </summary>
	public sealed class QueryConfigurationDesignManager : ConfigurationDesignManager
	{
		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="QueryConfigurationDesignManager"/> class.
		/// </summary>
		public QueryConfigurationDesignManager ()
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Registers the query design manager into the environment.
		/// </summary>
		/// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		public override void Register (IServiceProvider serviceProvider)
		{
			QueryNodeMapRegistrar nodeRegistrar = new QueryNodeMapRegistrar(serviceProvider);
			nodeRegistrar.Register();
			QueryCommandRegistrar commandRegistrar = new QueryCommandRegistrar(serviceProvider);
			commandRegistrar.Register();
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Opens the query configuration from an application configuration file.
		/// </summary>
		/// <param name="serviceProvider">A mechanism for retrieving a service object; that is an object that provides custom support to other objects.</param>
		/// <param name="rootNode">The <see cref="ConfigurationApplicationNode"/> of the hierarchy.</param>
		/// <param name="section">The query configuration section or null if no section was found.</param>
		protected override void OpenCore (IServiceProvider serviceProvider, ConfigurationApplicationNode rootNode, ConfigurationSection section)
		{
			if ( null != section )
			{
				QuerySettingsNodeBuilder builder = new QuerySettingsNodeBuilder(serviceProvider, (QuerySettings)section);
				QuerySettingsNode querySettingsNode = builder.Build();
				SetProtectionProvider(section, querySettingsNode);

				rootNode.AddNode(querySettingsNode);
			}
		}

		/// <summary>
		/// Gets a <see cref="ConfigurationSectionInfo"/> object containing the Query Block's configuration information.
		/// </summary>
		/// <param name="serviceProvider">A mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		/// <returns>
		/// A <see cref="ConfigurationSectionInfo"/> object containing the Query Block's configuration information.
		/// </returns>
		protected override ConfigurationSectionInfo GetConfigurationSectionInfo (IServiceProvider serviceProvider)
		{
			ConfigurationNode rootNode = ServiceHelper.GetCurrentRootNode(serviceProvider);
			QuerySettingsNode node = null;
			if (rootNode != null)
				node = (QuerySettingsNode)rootNode.Hierarchy.FindNodeByType(rootNode, typeof(QuerySettingsNode));

			QuerySettings querySection = null;
			if ( node == null )
				querySection = null;
			else
			{
				QuerySettingsBuilder builder = new QuerySettingsBuilder(serviceProvider, node);
				querySection = builder.Build();
			}
			string protectionProviderName = GetProtectionProviderName(node);

			return new ConfigurationSectionInfo(node, querySection, QuerySettings.SectionName, protectionProviderName);
		}
		#endregion
	}
}